package com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.dto.AsphaltMatchThresholdDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchNameEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchThresholdEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchTypeEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.mapper.AsphaltMatchThresholdMapper;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchNameService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchThresholdService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchTypeService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.system.common.ConvertUtils;

/**
 * @ClassName AsphaltMatchThresholdServiceImp
 * @Description 级配上下限业务接口实现
 * @author linhuawei
 * @since 2021-09-06
 */
@Service
public class AsphaltMatchThresholdServiceImp extends
        ServiceImpl<AsphaltMatchThresholdMapper, AsphaltMatchThresholdEntity> implements AsphaltMatchThresholdService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private AsphaltMatchTypeService asphaltMatchTypeService;

    @Autowired
    private AsphaltMatchNameService asphaltMatchNameService;

    @Override
    public IPage<AsphaltMatchThresholdEntity> page(Map<String, Object> params) {
        IPage<AsphaltMatchThresholdEntity> page = baseServiceComponent.getPage(params,
                AsphaltMatchThresholdEntity.class);
        QueryWrapper<AsphaltMatchThresholdEntity> queryWrapper = baseServiceComponent.getQueryWrapper(params,
                AsphaltMatchThresholdEntity.class);
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("match_type", "match_name");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public AsphaltMatchThresholdEntity getByMatchName(String matchName) {
        if(ObjectUtils.isEmpty(matchName)) {
            return null;
        }
        QueryWrapper<AsphaltMatchThresholdEntity> queryWrapper = new QueryWrapper<AsphaltMatchThresholdEntity>();
        queryWrapper.eq("match_name", matchName);
        List<AsphaltMatchThresholdEntity> list = baseMapper.selectList(queryWrapper);
        return null == list || 0 == list.size() ? null : list.get(0);
    }

    @Override
    public AsphaltMatchThresholdDto getDtoById(String id) {
        // 获取上下限数据
        AsphaltMatchThresholdEntity entity = baseMapper.selectById(id);
        if (null == entity) {
            return null;
        }

        // 对象转化
        AsphaltMatchThresholdDto dto = ConvertUtils.sourceToTarget(entity, AsphaltMatchThresholdDto.class);

        // 获取所属级配类型筛孔数据
        AsphaltMatchTypeEntity matchType = asphaltMatchTypeService.getByType(entity.getMatchType());
        if (null != matchType) {
            dto.setSieveArray(matchType.getSieveArray());
        }
        return dto;
    }

    @Override
    public boolean save(AsphaltMatchThresholdEntity entity) {
        // 判断级配名称是否重复
        AsphaltMatchNameEntity matchNameEntity = asphaltMatchNameService.getByName(entity.getMatchName());
        if (null == matchNameEntity) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "配方名称[" + entity.getMatchName() + "]不存在");
        }

        // 判断是否已经设置
        QueryWrapper<AsphaltMatchThresholdEntity> queryWrapper = new QueryWrapper<AsphaltMatchThresholdEntity>();
        queryWrapper.eq("match_name", entity.getMatchName());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "配方名称[" + entity.getMatchName() + "]已经配置上下限信息");
        }

        // 设置级配类型
        entity.setMatchType(matchNameEntity.getType());

        // 处理不需要设置的参数（防止前端传递多余的数据）
        handlerSieve(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(AsphaltMatchThresholdEntity entity) {
        // 处理不需要设置的参数（防止前端传递多余的数据）
        handlerSieve(entity);
        return super.updateById(entity);
    }

    /**
     * @Title handlerSieve
     * @Description 处理不需要设置的参数（防止前端传递多余的数据）
     * @param entity
     */
    private void handlerSieve(AsphaltMatchThresholdEntity entity) {
        AsphaltMatchTypeEntity matchTypeEntity = asphaltMatchTypeService.getByType(entity.getMatchType());
        Set<String> fields = new HashSet<String>();
        if (null != matchTypeEntity && null != matchTypeEntity.getSieveArray()
                && 0 < matchTypeEntity.getSieveArray().size()) {
            JSONArray array = matchTypeEntity.getSieveArray();
            String code;
            for (int i = 0, len = array.size(); i < len; i++) {
                code = array.getString(i);
                if (ObjectUtils.isNotEmpty(code)) {
                    fields.add(code.replace(".", "d"));
                }
            }
        }

        if (!fields.contains("0d075")) {
            entity.setSieve0d075max(null);
            entity.setSieve0d075min(null);
        }
        if (!fields.contains("0d15")) {
            entity.setSieve0d15max(null);
            entity.setSieve0d15min(null);
        }
        if (!fields.contains("0d3")) {
            entity.setSieve0d3max(null);
            entity.setSieve0d3min(null);
        }
        if (!fields.contains("0d6")) {
            entity.setSieve0d6max(null);
            entity.setSieve0d6min(null);
        }
        if (!fields.contains("1d18")) {
            entity.setSieve1d18max(null);
            entity.setSieve1d18min(null);
        }
        if (!fields.contains("2d36")) {
            entity.setSieve2d36max(null);
            entity.setSieve2d36min(null);
        }
        if (!fields.contains("4d75")) {
            entity.setSieve4d75max(null);
            entity.setSieve4d75min(null);
        }
        if (!fields.contains("9d5")) {
            entity.setSieve9d5max(null);
            entity.setSieve9d5min(null);
        }
        if (!fields.contains("13d2")) {
            entity.setSieve13d2max(null);
            entity.setSieve13d2min(null);
        }
        if (!fields.contains("16")) {
            entity.setSieve16max(null);
            entity.setSieve16min(null);
        }
        if (!fields.contains("19")) {
            entity.setSieve19max(null);
            entity.setSieve19min(null);
        }
        if (!fields.contains("26d5")) {
            entity.setSieve26d5max(null);
            entity.setSieve26d5min(null);
        }
        if (!fields.contains("31d5")) {
            entity.setSieve31d5max(null);
            entity.setSieve31d5min(null);
        }
        if (!fields.contains("37d5")) {
            entity.setSieve37d5max(null);
            entity.setSieve37d5min(null);
        }
        if (!fields.contains("52")) {
            entity.setSieve52max(null);
            entity.setSieve52min(null);
        }
        if (!fields.contains("53")) {
            entity.setSieve53max(null);
            entity.setSieve53min(null);
        }
    }

}
